package com.demo.gateway.sentinel.factory;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@Slf4j
@Component
public class FlowControlFactory implements InitializingBean {
    private static ConcurrentHashMap<String, FlowControl> flowControls = new ConcurrentHashMap<>();

    FlowControlFactory(@Autowired List<FlowControl> flowControlList){
        // 注意限流器的单实例特性
        flowControlList.forEach(flowControl -> {
            log.debug("flowController:{}---->{}",flowControl.getName(),flowControl);
            flowControls.put(flowControl.getName(),flowControl);
        });
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        //初始化后的必要检查项可以写在这里
    }
    public FlowControl get(String name) {
        return flowControls.get(name);
    }
}
